(function flexible (window, document) {
  const docEl = document.documentElement;
  const dpr = window.devicePixelRatio || 1;

  // 设置body字体大小
  function setBodyFontSize () {
      if (document.body) {
          document.body.style.fontSize = '16px';
      } else {
          document.addEventListener('DOMContentLoaded', setBodyFontSize);
      }
  }
  setBodyFontSize();

  // 设置1rem = viewWidth / 10
  function setRemUnit () {
  // PC端最大宽度限制
      const clientWidth = docEl.clientWidth;
      const rem = clientWidth > 750 ? 750 / 10 : clientWidth / 10;
      docEl.style.fontSize = rem + 'px';
  }

  setRemUnit();

  // 页面大小变化时重置rem
  window.addEventListener('resize', setRemUnit);
  window.addEventListener('pageshow', function (e) {
      if (e.persisted) {
          setRemUnit();
      }
  });

  // 当设备像素比大于1时，对字体进行放大
  if (dpr >= 2) {
      const fakeBody = document.createElement('body');
      const testElement = document.createElement('div');
      testElement.style.border = '.5px solid transparent';
      fakeBody.appendChild(testElement);
      docEl.appendChild(fakeBody);
      if (testElement.offsetHeight === 1) {
          docEl.classList.add('hairlines');
      }
      docEl.removeChild(fakeBody);
  }
})(window, document);